package com.nova.bluetooth.helpUtils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;

public class MapUtils {

    public static final String BAIDU_PACKAGE = "com.baidu.BaiduMap";
    public static final String GAODE_PACKAGE = "com.autonavi.minimap";

    // 跳转到百度地图
    public static void openBaiduMap(Context context, double latitude, double longitude,
                                    String address) {
        String uri = "baidumap://map/marker?location=" + latitude + "," + longitude + "&title="
                + address + "&content=" + address + "&src=" + context.getPackageName();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        intent.setPackage(BAIDU_PACKAGE);
        context.startActivity(intent);
    }

    // 跳转到高德地图
    public static void openGaodeMap(Context context, double latitude, double longitude,
                                    String address) {

        String uri = "androidamap://viewMap?sourceApplication=" + context.getPackageName() +
                "&poiname=" + address
                + "&lat=" + latitude + "&lon=" + longitude + "&dev=0";
        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(uri));
        intent.setPackage(GAODE_PACKAGE);
        context.startActivity(intent);
    }

    public static boolean isAppInstalled(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException exception) {
            return false;
        }
    }

    // 将百度坐标系经纬度转换为火星坐标系经纬度(腾讯/高德地图经纬度)
    public static double[] bd09ToGcj02(double bdLat, double bdLng) {
        double x = bdLng - 0.0065;
        double y = bdLat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);
        double gcjLon = z * Math.cos(theta);
        double gcjLat = z * Math.sin(theta);
        return new double[]{gcjLat, gcjLon};
    }

    // 将高德坐标系经纬度转换为百度坐标系经纬度
    public static double[] gcj02ToBd09(double gcjLat, double gcjLon) {
        double x = gcjLon, y = gcjLat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * Math.PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * Math.PI);
        double bdLon = z * Math.cos(theta) + 0.0065;
        double bdLat = z * Math.sin(theta) + 0.006;
        return new double[]{bdLat, bdLon};
    }

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        // 地球半径（单位：千米）
        double earthRadius = 6371.0;

        // 将角度转换为弧度
        double radLat1 = Math.toRadians(lat1);
        double radLon1 = Math.toRadians(lon1);
        double radLat2 = Math.toRadians(lat2);
        double radLon2 = Math.toRadians(lon2);

        // 计算经纬度差值
        double deltaLat = radLat2 - radLat1;
        double deltaLon = radLon2 - radLon1;

        // 使用Haversine公式计算距离
        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
                + Math.cos(radLat1) * Math.cos(radLat2) * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 计算距离
        double distance = earthRadius * c;

        return distance;
    }
}
